package com.wondream.myframework.app.mybatis.plugin;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator;

public class CustomAbstractXmlElementGenerator extends AbstractXmlElementGenerator {
    @Override
    public void addElements(XmlElement parentElement) {
        // 1.增加base_query
        XmlElement sql = new XmlElement("sql");
        sql.addAttribute(new Attribute("id", "base_query"));
        // 2.添加where条件
        // 2.1.设置trim标签
        XmlElement selectTrimElement = new XmlElement("trim");
        selectTrimElement.addAttribute(new Attribute("prefix", "WHERE"));
        // 2.2.给trim标签设置prefixOverrides属性
        selectTrimElement.addAttribute(new Attribute("prefixOverrides", "AND|OR"));
        StringBuilder sb = new StringBuilder();
        for (IntrospectedColumn introspectedColumn:
             introspectedTable.getAllColumns()) {
            if(introspectedColumn.isJdbcCharacterColumn() && introspectedColumn.getLength()>=256){
//                System.out.println(introspectedColumn.toString()+" length is big than 64, not as a condition for find method");
                continue;
            }
            if(introspectedColumn.isJDBCDateColumn() || introspectedColumn.isJDBCTimeColumn()){
//                System.out.println(introspectedColumn.toString()+" is date type, not as a condition for find method");
                continue;
            }
            XmlElement selectNotNullElement = new XmlElement("if");
            sb.setLength(0);
            sb.append("null != ");
            sb.append(introspectedColumn.getJavaProperty());
            selectNotNullElement.addAttribute(new Attribute("test", sb.toString()));
            sb.setLength(0);
            // 添加and
            sb.append(" and ");
            // 添加别名t
            sb.append("t.");
            sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
            // 添加等号
            sb.append(" = ");
            sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
            selectNotNullElement.addElement(new TextElement(sb.toString()));
            selectTrimElement.addElement(selectNotNullElement);
        }
        sql.addElement(selectTrimElement);
        parentElement.addElement(sql);
        // 3. 公用select
        sb.setLength(0);
        sb.append("select ");
        sb.append("t.* ");
        sb.append("from ");
        sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
        sb.append(" t");
        TextElement selectText = new TextElement(sb.toString());

        // 4. 公用include
        XmlElement include = new XmlElement("include");
        include.addAttribute(new Attribute("refid", "base_query"));
        // 5. 增加list
        XmlElement list = new XmlElement("select");
        list.addAttribute(new Attribute("id", "findByCondition"));
        list.addAttribute(new Attribute("resultMap", "BaseResultMap"));
        list.addAttribute(new Attribute("parameterType", introspectedTable.getBaseRecordType()));
        list.addElement(selectText);
        list.addElement(include);
        parentElement.addElement(list);
    }
}
